package com.atguigu.dao.impl;

import com.atguigu.dao.UserDao;
import com.atguigu.pojo.User;
import com.atguigu.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;

import java.sql.SQLException;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/12/11 14:05
 * @注释 JDBC调用数据库进行增删改查等操作
 */
public class UserDaoImpl implements UserDao {

    @Override
    public User selectUserByName(String username) {
        //1.创建对象
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        //准备SQL语句
        String sql = "select * from sys_user where username = ?";
        //执行查询 获取结果
        try {
            User user = qr.query(sql, new BeanHandler<>(User.class), username);
            return user;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void updatePasswordById(Integer id, String password) {
        //创建对象
        QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
        //准备SQL
        String sql = "update sys_user set password = ? where id = ?";
        //执行查询 获取结果
        try {
            qr.update(sql,password,id);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
